package manager.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import manager.bean.LayData;
import manager.dao.DepartmentDao;
import manager.dao.StaffDao;
import manager.dao.UserDao;
import manager.util.SYS;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/staff/*")
public class StaffServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Object path = req.getAttribute(SYS.REQUEST_PATH);

        System.out.println(path.toString());
        if ("list".equals(path)){
            List list = list(req);
            int count = count(req);
            LayData layData = new LayData(count,list);
            String json = JSON.toJSONString(layData, SerializerFeature.WriteDateUseDateFormat);
            resp.getWriter().write(json);
            System.out.println(json);
        }else if ("add".equals(path)){
            //新增
            int count = add(req);
            resp.getWriter().write(""+count);
        }else if ("edit".equals(path)){
            //修改
            int count = edit(req);
            resp.getWriter().write(""+count);
        }else if ("delete".equals(path)){
            //删除
            int count = delete(req);
            resp.getWriter().write(""+count);
        }else if ("initstafflist".equals(path)){
            List list = initStaffList(req);
            String json = JSON.toJSONString(list);
            resp.getWriter().write(json);
        }else if ("editstate".equals(path)){
            int count = editstate(req);
            System.out.println(count);
            resp.getWriter().write(""+count);
        }
        else {
            //无效的
        }

    }
    private StaffDao dao = new StaffDao();
    private List list(HttpServletRequest request){
        List list = null;
        String checktext = request.getParameter("checktext");
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        int start = (page - 1) * limit;
        String sql = "select * from staff ";
        if (checktext!=null){
            sql += " where name like ?  limit ?,?";
            list = dao.getStaff(sql,"%"+checktext+"%",start,limit);
        }else {
            sql += "limit ?,? ";
            System.out.println(sql);
            list = dao.getStaff(sql,start,limit);
        }
        return list;
    }

    private int count(HttpServletRequest request){
        String checktext = request.getParameter("checktext");
        int count = 0;
        String sql = "select count(*) from staff";
        if (checktext!=null){
            sql += "where name like ? ";
            count = dao.countStaff(sql,"%"+checktext+"%");
        }else {
            count = dao.countStaff(sql);
        }
        return count;

    }

    private int editstate(HttpServletRequest request){
        String username = request.getParameter("username");
        String sql1 = "select staff_id from user where username = ?";
        UserDao userDao = new UserDao();
        int id = userDao.state(sql1,username);
        String updata = "update staff set userstate = 1 where id = ?";
        System.out.println(updata);
        System.out.println(id);
        return dao.editStaff(updata,id);

    }

    private int delete(HttpServletRequest request){
        String str = request.getParameter("id");
        System.out.println(str);
        int count = 0;
        if (str!=null) {
            String sql = "delete from staff where id = '" + str + "'";
            System.out.println(sql);
            count = dao.deleteStaff(sql);
        }
        return count;
    }
    private int edit(HttpServletRequest request){
        String name = request.getParameter("name");
        String id = request.getParameter("id");
        String sex = request.getParameter("sex");
        String birthday = request.getParameter("birthday");
        String department_id = request.getParameter("department_id");
        String userstate = request.getParameter("userstate");
        String sql = "update staff set name = ? , sex = ?,birthday=? , department_id = ? , userstate = ?  where id = "+id;

        int count = dao.editStaff(sql,name,sex,birthday,department_id,userstate);
        return count;
    }
    private int add(HttpServletRequest request){
//        String id = request.getParameter("id");
        String code = request.getParameter("code");
        String name = request.getParameter("name");
        String sex = request.getParameter("sex");
        String birthday = request.getParameter("birthday");
        String department_id = request.getParameter("department_name");
//        String sqld = "select name from department where id = ?";
//        DepartmentDao dao1 = new DepartmentDao();
//        dao1.getid(sqld);
//        String userstate = request.getParameter("userstate");
        String sql = "insert staff (code,name,sex,birthday,department_id) values(?,?,?,?,?)";
        System.out.println(sql+code+name+sex+birthday+department_id);
        int count = dao.addStaff(sql,code,name,sex,birthday,department_id);
        return count;
    }

    private List initStaffList(HttpServletRequest request){
        String sql = "select a.* from staff a left join user b on a.id=b.staff_id where  b.id is null";
        return dao.getStaff(sql);
    }

}
